//
//  HJPictureView.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/9/27.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit

private let cellID = "HMPictureViewCellId"

class HJPictureView: UICollectionView {

    var pictureInfos: [HJStatusPictureInfo]? {
        didSet {
            //外界给pictureInfos赋值的时候刷新数据
            reloadData()
            
        }
    }
    
    
    override func awakeFromNib() {
        
        self.dataSource = self
        
        //注册cell
        self.register(HJPictureCell.self, forCellWithReuseIdentifier: cellID)
        
    }
}

//配图视图的数据源方法
extension HJPictureView : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfos?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! HJPictureCell
        //设置数据源
        cell.pictureInfo = pictureInfos![indexPath.item]
        
        
       // print(pictureInfos?.count)
        
        return cell
    }
    
    
}

class HJPictureCell: UICollectionViewCell {
    
    var pictureInfo: HJStatusPictureInfo? {
        didSet {
            let urlString = URL(string: pictureInfo?.wap_pic ?? "")
            imageView.sd_setImage(with: urlString)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加子视图
        self.contentView.addSubview(imageView)
        
        //设置约束
        imageView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self.contentView.snp.edges)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var imageView :UIImageView = {
    
        let iv = UIImageView()
        //设置图片样式
        iv.contentMode = .scaleAspectFill
        //裁剪图片
        iv.clipsToBounds = true
        return iv
    
    }()
    
}





